<form class="layui-form">
    <input type="hidden" name="id" value="${data.id!}">
    <table class="layui-table layui-table-content" lay-skin="nob">
        <tbody>
        <tr>
            <th width="150"><span class="red">*</span>名称</th>
            <td><input type="text" name="name" placeholder="请输入名称" lay-verify="required" autocomplete="off"
                       class="layui-input" value="${data.name!}"></td>
        </tr>
        <tr>
            <th><span class="red">*</span>类型</th>
            <td class="clear">
                <div class="fl">
                    <select name="category" lay-filter="category" lay-verify="required">
                        <option value="1">目录</option>
                        <option value="2">菜单</option>
                        <option value="3">按钮</option>
                    </select>
                </div>
            </td>
        </tr>
        <tr>
            <th>父级</th>
            <td class="clear">
                <div class="fl">
                    <select name="parentid" lay-verify=""></select>
                </div>
            </td>
        </tr>
        <tr>
            <th><span class="red">*</span>排序</th>
            <td><input type="text" name="sort" lay-verify="number" value="${data.sort!}" placeholder="请输入排序"
                       autocomplete="off"
                       class="layui-input"></td>
        </tr>
        <tr>
            <th>状态</th>
            <td>
                <input type="checkbox" name="status" lay-verify="" value="1" data-val="${data.status!}"
                       lay-skin="switch" lay-text="开启|关闭">
            </td>
        </tr>
        <tr>
            <th>链接</th>
            <td><input type="text" name="links" placeholder="请输入链接" value="${data.links!}" autocomplete="off"
                       class="layui-input"></td>
        </tr>
        <tr>
            <th>权限标识</th>
            <td><input type="text" name="privilegemark" placeholder="请输入权限标识" value="${data.privilegemark!}"
                       autocomplete="off" class="layui-input">
            </td>
        </tr>
        </tbody>
    </table>
    <div class="layui-button">
        <button class="layui-btn" style=" min-width: 150px;" lay-submit lay-filter="formSubmit">提 交</button>
        <div class="layui-btn" style=" min-width: 150px;" onclick="location.href='/administrate/menu'">返 回</div>
    </div>
</form>
<#macro script>
    <script>
        layui.use(['jquery', 'form', 'laydate', 'layer', 'laypage', 'element'], function () {
            var $ = layui.jquery,
                form = layui.form;
            $(function () {
                if ($("input[name='id']").val() == "") {
                    /*设置默认排序*/
                    setSort(1);
                }
                $("select[name='category']").val('${data.category!}');
                form.render('select');
                setParent($("select[name='category']").val());
                $("select[name='parentid']").val('${data.parentid!}');
                form.render('select');
                if ($("input[name='status']").data("val") == 1) {
                    $("input[name='status']").attr("checked", true);
                }
                else {
                    $("input[name='status']").attr("checked", false);
                }
                form.render('checkbox');
            });
            form.on('select(category)', function (data) {
                setSort(data.value);
                setParent(data.value);
            });

            /*提交*/
            form.on('submit(formSubmit)', function (data) {
                if (data.field.status == null) {
                    data.field.status = 2;
                }
                var url;
                if (data.field.id == "") {
                    url = "/administrate/menu/edit/insert";
                } else {
                    url = "/administrate/menu/edit/update";
                }
                $.ajax({
                    type: "POST",
                    url: url,
                    data: data.field,
                    async: false,
                    dataType: "json",
                    success: function (result) {
                        if (result.code == 200) {
                            layer.confirm('是否继续编辑', {
                                btn: ['是', '否']
                            }, function () {
                                location.href = location.href;
                            }, function () {
                                location.href = "/administrate/menu";
                            });
                        }
                        else {
                            layer.msg(result.msg);
                        }
                    }
                });
                return false;
            });

            /*设置排序*/
            function setSort(category) {
                $.ajax({
                    type: "GET",
                    url: "/administrate/menu/edit/getsort",
                    data: {"category": category},
                    async: false,
                    dataType: "json",
                    success: function (result) {
                        if (result.code == 200) {
                            $("input[name='sort']").val(result.sort);
                        }
                    }
                });
            }

            /*设置父级*/
            function setParent(category) {
                $.ajax({
                    type: "GET",
                    url: "/administrate/menu/edit/getparents",
                    data: {"category": category},
                    async: false,
                    dataType: "json",
                    success: function (result) {
                        if (result.code == 200) {
                            var html = "";
                            $.each(result.data, function (i, item) {
                                html += "<option value='" + item.id + "'>" + item.name + "</option>";
                            });
                            $("select[name='parentid']").html(html);
                            form.render('select');
                        }
                    }
                });
            }

        });
    </script>
</#macro>